package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.BigDecimalPropertyEditor;
import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.form.SaleCostForm;
import com.feeyin.radar.crm.service.SaleCostService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class SaleCostController extends MultiActionController
{
  private SaleCostService saleCostService;
  private String saleCostManage;
  private String deptSaleCost;
  private String waitAuditSaleCost;
  private String returnSaleCost;
  private String addSaleCost;
  private String updateSaleCost;
  private String viewSaleCost;

  public SaleCostService getSaleCostService()
  {
    return this.saleCostService;
  }

  public void setSaleCostService(SaleCostService saleCostService) {
    this.saleCostService = saleCostService;
  }

  public String getSaleCostManage() {
    return this.saleCostManage;
  }

  public void setSaleCostManage(String saleCostManage) {
    this.saleCostManage = saleCostManage;
  }

  public String getDeptSaleCost() {
    return this.deptSaleCost;
  }

  public void setDeptSaleCost(String deptSaleCost) {
    this.deptSaleCost = deptSaleCost;
  }

  public String getAddSaleCost() {
    return this.addSaleCost;
  }

  public void setAddSaleCost(String addSaleCost) {
    this.addSaleCost = addSaleCost;
  }

  public String getWaitAuditSaleCost() {
    return this.waitAuditSaleCost;
  }

  public void setWaitAuditSaleCost(String waitAuditSaleCost) {
    this.waitAuditSaleCost = waitAuditSaleCost;
  }

  public String getReturnSaleCost() {
    return this.returnSaleCost;
  }

  public void setReturnSaleCost(String returnSaleCost) {
    this.returnSaleCost = returnSaleCost;
  }

  public String getUpdateSaleCost() {
    return this.updateSaleCost;
  }

  public void setUpdateSaleCost(String updateSaleCost) {
    this.updateSaleCost = updateSaleCost;
  }

  public String getViewSaleCost() {
    return this.viewSaleCost;
  }

  public void setViewSaleCost(String viewSaleCost) {
    this.viewSaleCost = viewSaleCost;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    String sFlag = request.getParameter("sFlag");
    String saleCostCode = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      saleCostCode = request.getParameter("saleCostCode");
    }
    model.put("sFlag", sFlag);
    model.put("saleCostCode", saleCostCode);

    Page page = new Page();
    page.getPageList(this.saleCostService.getPageSaleCostsCount(saleCostCode, "2", userId).intValue(), null, request);
    model.put("saleCosts", this.saleCostService.getPageSaleCosts(saleCostCode, "2", userId, page));

    return new ModelAndView(getSaleCostManage(), model);
  }

  public ModelAndView deptSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    String sFlag = request.getParameter("sFlag");
    String saleCostCode = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      saleCostCode = request.getParameter("saleCostCode");
    }
    model.put("sFlag", sFlag);
    model.put("saleCostCode", saleCostCode);

    Page page = new Page();
    page.getPageList(this.saleCostService.getPageDeptSaleCostsCount(saleCostCode, userId).intValue(), null, request);
    model.put("saleCosts", this.saleCostService.getPageDeptSaleCosts(saleCostCode, userId, page));

    return new ModelAndView(getDeptSaleCost(), model);
  }

  public ModelAndView waitAuditSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    Page page = new Page();
    page.getPageList(this.saleCostService.getPageSaleCostsCount(null, "1", userId).intValue(), null, request);
    model.put("saleCosts", this.saleCostService.getPageSaleCosts(null, "1", userId, page));
    return new ModelAndView(getWaitAuditSaleCost(), model);
  }

  public ModelAndView returnSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    Page page = new Page();
    page.getPageList(this.saleCostService.getPageSaleCostsCount(null, "3", userId).intValue(), null, request);
    model.put("saleCosts", this.saleCostService.getPageSaleCosts(null, "3", userId, page));
    return new ModelAndView(getReturnSaleCost(), model);
  }

  public ModelAndView preAddSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    model.put("saleCostCode", DateUtils.getDateByFormat("yyyyMMddHHmmss"));
    model.put("costTypes", this.saleCostService.getSaleCostTypeRanges());
    return new ModelAndView(getAddSaleCost(), model);
  }

  public ModelAndView addSaleCost(HttpServletRequest request, HttpServletResponse response, SaleCostForm saleCostForm)
  {
    boolean result = false;
    int success = 0;
    if (saleCostForm != null) {
      result = this.saleCostService.insertSaleCost(saleCostForm);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./saleCost.do?action=preAddSaleCost&result=" + success));
  }

  public ModelAndView preUpdateSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("sign", request.getParameter("sign"));
    model.put("result", request.getParameter("result"));
    model.put("costTypes", this.saleCostService.getSaleCostTypeRanges());
    String saleCostCode = request.getParameter("saleCostCode");
    if (!StringUtils.isEmpty(saleCostCode)) {
      model.put("saleCostCode", saleCostCode);
      model.put("totalCost", this.saleCostService.getTotalSaleCostBySaleCostCode(saleCostCode));
      model.put("saleCosts", this.saleCostService.getSaleCostsBySaleCostCode(saleCostCode));
    }
    return new ModelAndView(getUpdateSaleCost(), model);
  }

  public ModelAndView updateSaleCost(HttpServletRequest request, HttpServletResponse response, SaleCostForm saleCostForm)
  {
    String sign = request.getParameter("sign");
    boolean result = false;
    int success = 0;
    if (saleCostForm != null) {
      result = this.saleCostService.updateSaleCost(saleCostForm);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./saleCost.do?action=preUpdateSaleCost&result=" + success + "&saleCostCode=" + saleCostForm.getSaleCostCode() + "&sign=" + sign));
  }

  public ModelAndView deleteWaitSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String saleCostCode = request.getParameter("saleCostCode");
    if (!StringUtils.isEmpty(saleCostCode)) {
      result = this.saleCostService.deleteSaleCost(saleCostCode);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./saleCost.do?action=waitAuditSaleCost&result=" + success));
  }

  public ModelAndView deleteReturnSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String saleCostCode = request.getParameter("saleCostCode");
    if (!StringUtils.isEmpty(saleCostCode)) {
      result = this.saleCostService.deleteSaleCost(saleCostCode);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./saleCost.do?action=returnSaleCost&result=" + success));
  }

  public ModelAndView viewSaleCost(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("sign", request.getParameter("sign"));
    model.put("costTypes", this.saleCostService.getSaleCostTypeRanges());
    String saleCostCode = request.getParameter("saleCostCode");
    if (!StringUtils.isEmpty(saleCostCode)) {
      model.put("saleCostCode", saleCostCode);
      model.put("totalCost", this.saleCostService.getTotalSaleCostBySaleCostCode(saleCostCode));
      model.put("saleCosts", this.saleCostService.getSaleCostsBySaleCostCode(saleCostCode));
    }
    return new ModelAndView(getViewSaleCost(), model);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
  {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
    binder.registerCustomEditor(BigDecimal.class, new BigDecimalPropertyEditor());
  }
}